package com.hhd.pdf.component.table;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.component.AbstractPdfComponent;
import com.hhd.pdf.component.table.factory.TableBodyFactory;
import com.hhd.pdf.component.table.factory.TableHeaderFactory;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPTable;
import org.dom4j.Attribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TableComponent extends AbstractPdfComponent {

    @Autowired
    @Qualifier("tableHeaderFactory")
    TableHeaderFactory headerFactory;

    @Autowired
    TableBodyFactory bodyFactory;

    @Override
    public List<Element> doGenElements(GeneratorContext context) {
        org.dom4j.Element element = context.getParsingElement();
        TableVo tableVo = getTableParsed(element);
        if (CommonUtils.isEmpty(tableVo.getTrs())) return CommonUtils.newList();
        PdfPTable table = createTable(tableVo, element, context, tableVo);
        TableBorderHelper tableBorderHelper = new TableBorderHelper();
        buildTable(context, table, tableVo, tableBorderHelper, element);
        resolveDefaultPadding(context);
        return CommonUtils.newList(table);
    }

    private void resolveDefaultPadding(GeneratorContext context) {
        setDefaultAttribute(context.getParsingElement(), "paddingTop", "10");
        setDefaultAttribute(context.getParsingElement(), "paddingBottom", "10");
    }

    private void buildTable(GeneratorContext context,
                            PdfPTable table,
                            TableVo tableVo,
                            TableBorderHelper tableBorderHelper,
                            org.dom4j.Element element) {
        headerFactory.createTableElements(table, tableVo, context, tableBorderHelper, element);
        bodyFactory.createTableElements(table, tableVo, context, tableBorderHelper, element);
    }

    private TableVo getTableParsed(org.dom4j.Element element) {
        TableXmlParser tableXmlParser = new TableXmlParser(element);
        return tableXmlParser.getTable();
    }

    private PdfPTable createTable(TableVo tableVo, org.dom4j.Element element,
                                  GeneratorContext context,
                                  TableVo tableVo1) {
        return createTable(tableVo, element, context, tableVo.getTrs());
    }

    private PdfPTable createTable(TableVo table,
                                  org.dom4j.Element element,
                                  GeneratorContext context,
                                  List<TrVo> trs) {
        TrVo trVo = trs.get(0);
        List<TdVo> tds = trVo.getTds();
        int colNum = 0;
        colNum = getColNum(tds, colNum);
        return PdfUtils.createTable(colNum, context.getGeneratorParameters().getInitPageWidth());
    }

    private int getColNum(List<TdVo> tds, int colNum) {
        for (TdVo td : tds) {
            Attribute colSpan = td.getTd().attribute("colSpan");
            if (!CommonUtils.isEmpty(colSpan)) {
                String value = colSpan.getValue();
                colNum += Integer.parseInt(value);
            } else {
                colNum++;
            }
        }
        return colNum;
    }

    @Override
    public String getName() {
        return "table";
    }
}
